package Dao;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.Reader;

public class UserDaoImpl implements UserDao{
    SqlSessionFactoryBuilder builder =null;
    SqlSessionFactory factory=null;
    SqlSession sqlSession=null;
    Reader reader=null;
    public int getcount() {
        int result = -1;
        try {
            //读取配置文件
            reader = Resources.getResourceAsReader("mybatis-config.xml");
            //构建builder
            builder = new SqlSessionFactoryBuilder();
            //构建factory
            factory = builder.build(reader);
            //构建sqlSession
            sqlSession = factory.openSession();
            //执行数据库操作  （dao接口的全限定名+id）
//            result = sqlSession.selectOne("Dao.UserDao.findall");
            result =sqlSession.getMapper(UserDao.class).getcount();
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }finally {
            sqlSession.close();
        }
        return result;
    }
}
